package uscdb.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import uscdb.dao.EnvironmentDaoI;
import uscdb.model.Tenvironment;
import uscdb.pageModel.DataGrid;
import uscdb.pageModel.Json;
import uscdb.pageModel.Lenvironment;
import uscdb.pageModel.PageHelper;
import uscdb.service.EnvironmentServiceI;

@Service
public class EnvironmentServiceImpl implements EnvironmentServiceI {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(EnvironmentServiceImpl.class);

	@Autowired
	private EnvironmentDaoI environmentDao;

	@Override
	public void add(Lenvironment lenvironment) {
		lenvironment.setId(UUID.randomUUID().toString());
		;
		Tenvironment tenvironment = new Tenvironment();
		BeanUtils.copyProperties(lenvironment, tenvironment);
		environmentDao.save(tenvironment);

	}

	@Override
	public void edit(Lenvironment lenvironment) {
		Tenvironment tenvironment = environmentDao.get(Tenvironment.class, lenvironment.getId());
		try {
			if (null != tenvironment) {
				BeanUtils.copyProperties(lenvironment, tenvironment);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public Json delete(String ids) {
		Json json = new Json();
		for (String id : ids.split(",")) {
			Tenvironment tenvironment = environmentDao.get(Tenvironment.class, id);
			environmentDao.delete(tenvironment);
		}
		json.setMsg("删除成功");
		json.setSuccess(true);
		return json;
	}

	@Override
	public DataGrid dataGrid(Lenvironment lenvironment, PageHelper ph) {
		DataGrid dg = new DataGrid();
		List<Lenvironment> lenvironmentList = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();
		String hql = " from Tenvironment t ";
		List<Tenvironment> tenvironmentList = environmentDao.find(hql + whereHql(lenvironment, params) + orderHql(ph),
				params, ph.getPage(), ph.getRows());
		for (Tenvironment tenvironment : tenvironmentList) {
			Lenvironment lenvironment2Page = new Lenvironment();
			BeanUtils.copyProperties(tenvironment, lenvironment2Page);
			lenvironmentList.add(lenvironment2Page);
		}
		dg.setRows(lenvironmentList);
		dg.setTotal(environmentDao.count("select count(*) " + hql + whereHql(lenvironment, params), params));
		return dg;
	}

	/**
	 * 排序函数
	 * 
	 * @param ph
	 *            分页对象
	 * @return
	 */
	private String orderHql(PageHelper ph) {
		String orderString = "";
		if (ph.getSort() != null && ph.getOrder() != null) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	private String whereHql(Lenvironment lenvironment, Map<String, Object> params) {
		String hql = "";
		if (null != lenvironment) {
			hql += " where 1=1 ";
		}
		return hql;
	}

}
